"""
    *@description: sovle the top N problem using heap
    *@author: timlentse(tinglenxan@gmail.com)
    *@Date:2014-11-15
"""
#! usr/bin/python
import heapq
def heapSearch( bigArray, N ):
    heap = []
    # Note: below is for illustration. It can be replaced by 
    # heapq.nlargest( bigArray, N )
    for item in bigArray:
        # If we have not yet found N items, or the current item is larger than
        # the smallest item on the heap,
        if len(heap) < N:
            heapq.heappush( heap, item )
        # If the heap is full, remove the smallest element on the heap.
        else:
            if item > heap[0]:
                heapq.heappop( heap )
            # add the current element as the new smallest.
                heapq.heappush( heap, item )
    return heap
testArray = [10,1,3,4,5,6,7,8,9,4,5,6,7,11]
print heapSearch(testArray,5)
